from django.db import models
from django.contrib.auth.models import User


class UserProfile(models.Model):
    """用户扩展信息"""
    
    id = models.BigAutoField(
        primary_key=True,
        verbose_name='编号',
        db_comment='扩展ID'
    )
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        related_name='profile',
        verbose_name='用户',
        db_comment='关联用户'
    )
    phone = models.CharField(
        max_length=11,
        unique=True,
        null=True,
        blank=True,
        verbose_name='手机号码',
        db_comment='手机号码'
    )
    avatar = models.ImageField(
        upload_to='avatars/',
        null=True,
        blank=True,
        verbose_name='头像',
        db_comment='头像'
    )
    nickname = models.CharField(
        max_length=50,
        null=True,
        blank=True,
        verbose_name='昵称',
        db_comment='昵称'
    )
    school = models.CharField(
        max_length=100,
        verbose_name='学校',
        db_comment='学校名称'
    )
    major = models.CharField(
        max_length=100,
        null=True,
        blank=True,
        verbose_name='专业',
        db_comment='专业'
    )
    grade = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='年级',
        db_comment='年级'
    )
    student_id = models.CharField(
        max_length=20,
        null=True,
        blank=True,
        verbose_name='学号',
        db_comment='学号'
    )
    is_verified = models.BooleanField(
        default=False,
        verbose_name='已认证',
        db_comment='是否已认证'
    )
    credit_score = models.DecimalField(
        max_digits=5,
        decimal_places=2,
        default=100.00,
        verbose_name='信用分数',
        db_comment='信用分数'
    )
    volunteer_hours = models.DecimalField(
        max_digits=6,
        decimal_places=1,
        default=0.0,
        verbose_name='志愿时长',
        db_comment='志愿时长(小时)'
    )
    bio = models.TextField(
        null=True,
        blank=True,
        verbose_name='个人简介',
        db_comment='个人简介'
    )
    preferences = models.JSONField(
        default=dict,
        blank=True,
        verbose_name='偏好设置',
        db_comment='用户偏好设置'
    )
    favorite_categories = models.JSONField(
        default=list,
        blank=True,
        verbose_name='喜欢的分类',
        db_comment='喜欢的书籍分类'
    )
    reading_interests = models.JSONField(
        default=list,
        blank=True,
        verbose_name='阅读兴趣',
        db_comment='阅读兴趣标签'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间',
        db_comment='创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间',
        db_comment='更新时间'
    )
    
    class Meta:
        db_table = 'campus_user_profile'
        db_table_comment = '用户扩展信息表'
        verbose_name = '用户扩展信息'
        verbose_name_plural = '用户扩展信息'
        ordering = ['-created_at']
    
    def __str__(self):
        return f'{self.user.username}的扩展信息'
